var Report = {

    selected_row_id: null,
    objGrid: null,

    init: function () {
        var self = this;

        self.configUI();

        $("#branch").change(function() {
            var value = $(this).val();
            var select = document.getElementById('teller');
            select.options.length = 0;
            if( typeof branch_tellers[value] == "undefined" ) {
                var opt = document.createElement('option');
                opt.value = 0;
                opt.text = 'All Tellers';
                select.add(opt, null);
                return;
            }
            var d_options = branch_tellers[value];
            //All Tellers
            var opt = document.createElement('option');
            opt.value = 0;
            opt.text = 'All Tellers';
            try { //Standard
                select.add(opt, null);
            }
            catch( error ) { //IE Only
                select.add(opt);
            }
            for(var nx in d_options) {
                var opt = document.createElement('option');
                opt.value = d_options[nx]['id'];
                opt.text = d_options[nx]['fname'] + " " + d_options[nx]['mname']+ " " + d_options[nx]['lname'];
                try { //Standard
                    select.add(opt, null);
                }
                catch( error ) { //IE Only
                    select.add(opt);
                }
            }
            $('#teller').val(selected_teller);
        }).change();

        self.objGrid = $('#flex').flexigrid({
            colModel: [
                {display: 'Date', width: 100,align: 'left'},
                {display: 'Reference', width: 100,align: 'left'},
                {display: 'Branch', width: 120,align: 'left'},
                {display: 'Teller', width: 180, align: 'center'},
                {display: 'Credit GHc', width: 150, align: 'center'},
                {display: 'Option', width: 100, align: 'center'}
            ],
            height:500,
            subgrid: {
                use: false
            }
        });

        $("#export-btn").click(function () {
            self.print_export_Details('export');
        });
        $("#print-btn").click(function () {
            self.print_export_Details('print');
        });

        $("#start_dt,#end_dt").mask("9999-99-99");
    },


    print_export_Details: function(data_type){
        $("#print-export-form #data_type").val(data_type);
        $("#print-export-form #data_trans_ref_no").val($("#form-query #trans_ref_no").val());
        $("#print-export-form #data_start_dt").val($("#form-query #start_dt").val());
        $("#print-export-form #data_end_dt").val($("#form-query #end_dt").val());
        $("#print-export-form #data_branch").val($("#form-query #branch").val());
        $("#print-export-form #data_teller").val($("#form-query #teller").val());
        window.open('', "PrintExportWindow", "menubar=yes, width=600, height=500,location=no,status=no,scrollbars=yes,resizable=yes");
        $("#print-export-form").submit();
    },

    /**
     * Sets up UI for results interface
     */
    configUI: function() {
        this.configRevertModal();
        this.configTransRows();
    },


    /**
     * Sets up UI elements for the Transactions display on the right
     */
    configTransRows: function() {
        $("#message_modal_close_callback").click(function(e){
            $("#re_query-btn").click();
        });

        $(".revert_btn").live('click', function(e) {
            e.preventDefault();
            var tr = $(this).parents('tr');

            $("#trans_reversal_modal form").get(0).reset();
            $("#trans_reversal_modal").find('#pay_method').html( tr.attr("data-payment_method") );
            $("#trans_reversal_modal").find('#amount').html( tr.attr("data-amount") );
            $("#trans_reversal_modal").find('#transaction_debit_amt').val( tr.attr("data-amount") );
            $("#trans_reversal_modal").find("#transaction_id").val( tr.attr("data-trans-id") );
            $("#trans_reversal_modal").find("#student_id").val( tr.attr("data-stu-id") );

            $("#trans_reversal_modal").modal('show');
            setTimeout(function() {
                $("#trans_reversal_modal textarea").focus();
            }, 500);
        });
    },

    /**
     * sets up UI for teh revert transaction modal
     * @returns {undefined}
     */
    configRevertModal: function() {
        var self = this;
        $("#trans_reversal_modal form").submit(function(e) {
            e.preventDefault();
            self.revertTransaction();
        });
    },


    /**
     * Triggers the AJAX call to make a transaction as requiring reversal
     */
    revertTransaction: function() {
        var self = this;
        if( !confirm("You are about to revert this transaction and potentially refund the amount paid.\n\nClick OK to continue or CANCEL to return to previous screen")) {
            return;
        }

        var query = $("#trans_reversal_modal form").serialize();
        var url = $("#trans_reversal_modal form").attr("action");

        $.ajax({
            type: 'post',
            url: url,
            data: query,
            dataType: 'json',
            success: function(data) {
                $("#trans_reversal_modal").modal("hide");
                if(data.status == 'success'){
                    self.showMessage("Revert Transaction", data.message);
                }
                else{
                    self.showMessage("Failed To Revert Transaction", data.message);
                }
            },
            error: function(xhr) {
                console.log(xhr.responseText);
            }
        });
    },


    /**
     * Displays an alert message
     *
     * @param {type} title
     * @param {type} message
     * @param {type} type
     * @returns {undefined}
     */
    showMessage: function(title, message, type) {
        $("#message_modal .modal-header h4").html(title);
        $("#message_modal .modal-body").html(message);

        $("#message_modal").modal('show');
    }

};

/* when the page is loaded */
$(document).ready(function () {
    Report.init();
});